# MenuOverlay - Texture that is displayed over video when menu is active
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import gtk
import clutter

class MenuOverlay(clutter.Texture):
    """
    Menu overlay widget
    
    Simple texture that is displayed over video when menu and video are active
    at the same time. Only difference to normal texture is that this texture
    provides methods fade_in() and fade_out().
    """
    
    #TODO: If user switches quickly from OSD to menu and back, then animation
    #      flickers because the first animation doesn't have time to finish.
    #      This should be fixed! Detect if timeline is still running, stop it etc.
    
    def __init__(self, theme):
        """Initialize overlay texture."""
        clutter.Texture.__init__(self)
        pixbuf = gtk.gdk.pixbuf_new_from_file(theme.getImage("menu_overlay"))
        self.set_pixbuf(pixbuf)
        self.timeline = clutter.Timeline(13, 26)
        self.timeline.set_loop(False)
    
    def fade_in(self):
        """
        Fade texture in. Animate texture's opacity from 0 to 255
        """
        self.alpha = clutter.Alpha(self.timeline, clutter.smoothstep_dec_func)
        self.behaviour = clutter.BehaviourOpacity(255, 0, self.alpha)
        self.behaviour.apply(self)
        self.timeline.rewind()
        self.timeline.start()
        
    def fade_out(self):
        """
        Fade texture out. Animate texture's opacity from 255 to 0
        """
        self.alpha = clutter.Alpha(self.timeline, clutter.smoothstep_inc_func)
        self.behaviour = clutter.BehaviourOpacity(255, 0, self.alpha)
        self.behaviour.apply(self)
        self.timeline.rewind()
        self.timeline.start()
        